package com.itboye.textileexpres.login;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.ItemBeanAdapter;
import com.itboye.textileexpes.adapter.SearchCityAdapter;
import com.itboye.textileexpes.beans.LogonCity;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.widget.ClearEditText;
import com.itboye.textileexpres.widget.MyLetterSortView;
import com.itboye.textileexpres.widget.MyLetterSortView.OnTouchingLetterChangedListener;
import com.itboye.textillexepres.db.MyCityDBHelper;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class RegionActivity extends BaseActivity implements OnClickListener {
	public static final String TAG = RegionActivity.class.getSimpleName();

	private Context context = RegionActivity.this;

	private ClearEditText mClearEditText;
	private TextView tv_mid_letter;
	private ListView listView;
	private MyLetterSortView right_letter;

	private ItemBeanAdapter mAdapter;
	private List<LogonCity> mlist = new ArrayList<LogonCity>();
	private InputMethodManager inputMethodManager;

	private View mCityContainer;
	private FrameLayout mSearchContainer;
	private ListView mSearchListView;
	private SearchCityAdapter mSearchCityAdapter;
	private ImageView back, img_other;
	private TextView title;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_reagion;
	}

	@Override
	public void onMyCreate() {
		initVieww();
		setLinstener();
		initData();
		fillData();
		img_other.setVisibility(View.GONE);
		title.setText("选择国家和地区");
	}

	protected void initData() {

		getDataCity();
		mAdapter = new ItemBeanAdapter(this, mlist);
		listView.setEmptyView(findViewById(R.id.citys_list_load));
		listView.setAdapter(mAdapter);

	}

	protected void initVieww() {

		inputMethodManager = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		listView = (ListView) findViewById(R.id.list);
		mClearEditText = (ClearEditText) findViewById(R.id.et_msg_search);
		// 这里设置中间字母
		right_letter = (MyLetterSortView) findViewById(R.id.right_letter);
		tv_mid_letter = (TextView) findViewById(R.id.tv_mid_letter);
		right_letter.setTextView(tv_mid_letter);
		// 搜索
		mCityContainer = findViewById(R.id.city_content_container);
		mSearchContainer = (FrameLayout) this
				.findViewById(R.id.search_content_container);
		mSearchListView = (ListView) findViewById(R.id.search_list);
		mSearchListView.setEmptyView(findViewById(R.id.search_empty));
		mSearchContainer.setVisibility(View.GONE);

	}

	protected void setLinstener() {

		// tv_reget_pwd.setOnClickListener(this);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// toast(getApplicationContext(),
				// ((City) mAdapter.getItem(position)).toString());
				// ByAlert.alert(getApplicationContext(),((LogonCity)mAdapter.getItem(position)).toString());
				// SPUtils.put(RegionActivity.this, null,
				// SpPublicParams.CITYCOUNTRY_TEL_PREFIXUID,
				// mlist.get(position).getNumber() + "");
				// SPUtils.put(RegionActivity.this, null,
				// SpPublicParams.COUNTRYNMAW, mlist.get(position)
				// .getProvince() + "");
				// Intent intentt = new Intent("country");
				// sendBroadcast(intentt);
				Intent intent = new Intent();
				intent.putExtra("number", mlist.get(position).getNumber());
				intent.putExtra("province", mlist.get(position).getProvince());
				setResult(104, intent);
				finish();

			}
		});

		listView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 隐藏软键盘
				if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
					if (getCurrentFocus() != null)
						inputMethodManager.hideSoftInputFromWindow(
								getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
				}
				return false;
			}
		});

		// 设置右侧触摸监听
		right_letter
				.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

					@Override
					public void onTouchingLetterChanged(String s) {
						// 该字母首次出现的位置
						int position = mAdapter.getPositionForSection(s
								.charAt(0));
						if (position != -1) {
							listView.setSelection(position);
						}

					}
				});

		// 根据输入框输入值的改变来过滤搜索
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
				filterData2(s.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		mSearchListView
				.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// SPUtils.put(RegionActivity.this, null,
						// SpPublicParams.CITYCOUNTRY_TEL_PREFIXUID,
						// mSearchCityAdapter.getItem(position).getNumber()+
						// "");
						// SPUtils.put(RegionActivity.this, null,
						// SpPublicParams.COUNTRYNMAW,
						// mSearchCityAdapter.getItem(position).getProvince() +
						// "");
						// Intent intentt = new Intent("country");
						// sendBroadcast(intentt);
						// finish();
						Intent intent = new Intent();
						intent.putExtra("number",
								mSearchCityAdapter.getItem(position)
										.getNumber());
						intent.putExtra("province",
								mSearchCityAdapter.getItem(position)
										.getProvince());
						setResult(104, intent);
						finish();
					}
				});
	}

	protected void fillData() {
		// TODO Auto-generated method stub

	}

	private void getDataCity() {

		new Thread(new Runnable() {

			@Override
			public void run() {
				MyCityDBHelper myCityDBHelper = new MyCityDBHelper(context);

				mlist = myCityDBHelper.getCityDB().getAllCity();
				mHandler.sendEmptyMessage(0);

			}
		}).start();

		// 对list进行排序
		// Collections.sort(mlist, new PinyinComparator() {
		// });

	}

	private void filterData2(String filterStr) {
		mSearchCityAdapter = new SearchCityAdapter(RegionActivity.this, mlist);
		mSearchListView.setAdapter(mSearchCityAdapter);
		mSearchListView.setTextFilterEnabled(true);
		if (mlist.size() < 1 || TextUtils.isEmpty(filterStr)) {
			mCityContainer.setVisibility(View.VISIBLE);
			mSearchContainer.setVisibility(View.INVISIBLE);

		} else {

			mCityContainer.setVisibility(View.INVISIBLE);
			mSearchContainer.setVisibility(View.VISIBLE);
			mSearchCityAdapter.getFilter().filter(filterStr);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		//
		default:
			break;
		}

	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:

				mAdapter.updateListView(mlist);
				break;
			default:
				break;
			}
		}
	};
}
